package com.tengju.bff.interfaces.shared;

import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.settlement.domain.shared.Amount;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Formatter {


    private Formatter() {
    }

    public static String beautyMoney(Amount amount) {
        return String.format("%.2f", amount.getValue());
    }

    public static String thousandMoneyOfTenMillion (int money) {
        if (money > 10000000) {
            return "1000W+";
        } else {
            return thousandMoney(money);
        }
    }

    // 百位抹0
    public static String formatIndex(int index) {
        index = (index / 100) * 100;
        return thousandMoneyOfTenMillion(index);
    }

    public static String thousandMoney (int money) {
        DecimalFormat format = new DecimalFormat("###,###");
        format.setRoundingMode(RoundingMode.DOWN);
        return format.format(money);
    }

    public static String thousandMoney (double money) {
        DecimalFormat format = new DecimalFormat("###,###.##");
        format.setRoundingMode(RoundingMode.DOWN);
        return format.format(money);
    }

    public static String thousandObj(Object obj) {
        if (obj == null) {
            return "-";
        }
        BigDecimal bigDecimal = null;
        if (obj instanceof BigDecimal) {
            bigDecimal = (BigDecimal) obj;
        } else if (obj instanceof String) {
            bigDecimal = new BigDecimal((String) obj);
        } else if (obj instanceof BigInteger) {
            bigDecimal = new BigDecimal((BigInteger) obj);
        } else if (obj instanceof Number) {
            bigDecimal = new BigDecimal(((Number) obj).doubleValue());
        } else {
            return "-";
        }

        DecimalFormat format = new DecimalFormat("###,###");
        format.setRoundingMode(RoundingMode.DOWN);
        return format.format(bigDecimal);

    }


    public static String numberFormat(Object obj, String formatPattern) {
        if (obj == null || StringUtils.isBlank(formatPattern)) {
            return "-";
        }
        BigDecimal bigDecimal = null;
        if (obj instanceof BigDecimal) {
            bigDecimal = (BigDecimal) obj;
        } else if (obj instanceof String) {
            bigDecimal = new BigDecimal((String) obj);
        } else if (obj instanceof BigInteger) {
            bigDecimal = new BigDecimal((BigInteger) obj);
        } else if (obj instanceof Number) {
            bigDecimal = BigDecimal.valueOf(Double.valueOf(obj.toString()));
        } else {
            return "-";
        }

        DecimalFormat format = new DecimalFormat(formatPattern);
        format.setRoundingMode(RoundingMode.DOWN);
        return format.format(bigDecimal);

    }

    public static String numberFormatTenThousand(String number){
        double v;
        try {
            v = Double.parseDouble(number);
        }catch (Exception e){
            return "-";
        }
        if (StringUtils.isBlank(number)){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal(v);
        // 转换为万元（除以10000）
        if (Double.parseDouble(number) > 10000){
            BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
            // 保留两位小数
            DecimalFormat formater = new DecimalFormat("0.00");
            formater.setRoundingMode(RoundingMode.DOWN);
            String res = formater.format(decimal);
            String lastTwoNumber = StringUtils.substring(res, res.length() - 2, res.length());
            // 四舍五入
//            formater.setRoundingMode(RoundingMode.HALF_DOWN);
//            formater.setRoundingMode(RoundingMode.HALF_EVEN);
            String s = commaMoney(new BigDecimal(formater.format(decimal)));
            return number =  s + "." + lastTwoNumber + "W";
        }
        return commaMoney(new BigDecimal(number));  // 初始过滤两位小数
    }
    /**
     * eg:2,333万, 2,333
     * @param obj
     * @return
     */
    public static String tenThousandMoney(Object obj) {
        if (obj == null) {
            return "-";
        }
        long money = BigDecimal.valueOf(CastUtil.castDouble(obj)).longValue();
        DecimalFormat format = new DecimalFormat("###,###.##");
        format.setRoundingMode(RoundingMode.DOWN);
        if (money > 10000) {
            return format.format((double)money / 10000) + "万";
        } else {
            return format.format(money);
        }
    }

    public static String percent(double factor, int digits) {
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(digits);
        return nf.format(factor);
    }

    public static String percentOfObj(Object obj, int digits) {
        BigDecimal bigDecimal = CastUtil.castBigDecimal(obj);
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(digits);
        return nf.format(bigDecimal);
    }

    //逗号分割金额 暂时去掉两位小数
    public static String commaMoney (BigDecimal bigDecimal) {
        if(bigDecimal == null) {
            return "0";
        }
        if (BigDecimal.ZERO.compareTo(bigDecimal) == 0) {
            return "0";
        }
        DecimalFormat df = new DecimalFormat("#,###");
        df.setRoundingMode(RoundingMode.DOWN);
        return df.format(bigDecimal);

    }

    //逗号分割人数 暂时去掉两位小数
    public static String commaPeople(String peopleCnt) {
        double v;
        try {
            v = Double.parseDouble(peopleCnt);
        }catch (Exception e){
            return "-";
        }
        if (StringUtils.isBlank(peopleCnt)){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal(v);
        if (BigDecimal.ZERO.compareTo(bigDecimal) == 0) {
            return "0";
        }
        DecimalFormat df = new DecimalFormat("#,###");
        df.setRoundingMode(RoundingMode.DOWN);
        return df.format(bigDecimal);
    }

    public static void main(String[] args) {
        System.out.println(calRate("10","99"));
    }

    // 计算百分比, 保留两位小数, 四舍五入
    public static String calRate(String number1, String number2){
        double v1, v2;
        try {
             v1 = Double.parseDouble(number1);
             v2 = Double.parseDouble(number2);
        }catch (Exception e){
            return "-" ;
        }

        if (v2 == 0 || v1 == 0){
            return "-";
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(v1 / v2);
        // 保留两位小数
        DecimalFormat formater = new DecimalFormat("0.0000");
        formater.setRoundingMode(RoundingMode.HALF_UP);
        String res = formater.format(bigDecimal);
        BigDecimal resDigit = new BigDecimal(Double.parseDouble(res) * 100);
        DecimalFormat resFormat = new DecimalFormat("0.00");
        String result = resFormat.format(resDigit);
        return result + "%";
    }

    public static String formatObjOfTenMillion(Object obj) {

        if (obj == null) {
            return "";
        }

        BigDecimal bigDecimal = null;
        if (obj instanceof BigDecimal) {
            bigDecimal = (BigDecimal) obj;
        } else if (obj instanceof String) {
            bigDecimal = new BigDecimal((String) obj);
        } else if (obj instanceof BigInteger) {
            bigDecimal = new BigDecimal(((BigInteger) obj));
        } else if (obj instanceof Number) {
            bigDecimal = BigDecimal.valueOf(((Number) obj).doubleValue());
        } else {
            return "";
        }

        if (BigDecimal.ZERO.compareTo(bigDecimal) == 0) {
            return "0";
        }
        BigDecimal resultDecimal = bigDecimal.divide(BigDecimal.valueOf(10000));

        DecimalFormat format = new DecimalFormat("###,##");
        format.setRoundingMode(RoundingMode.DOWN);
        if (resultDecimal.compareTo(BigDecimal.valueOf(1000)) > 0) {
            return String.format("%sW+", format.format(BigDecimal.valueOf(1000)));
        }
        bigDecimal.setScale(2, RoundingMode.FLOOR);
        return bigDecimal.toString();

    }

    public static String thousandDecimal(BigDecimal bigDecimal) {
        if(bigDecimal == null) {
            return "0";
        }
        if (BigDecimal.ZERO.compareTo(bigDecimal) == 0) {
            return "0";
        }
        DecimalFormat format = new DecimalFormat("###,###");
        format.setRoundingMode(RoundingMode.DOWN);
        return format.format(bigDecimal);
    }

    public static Object formatter(Meta meta,Object val) {
        if (meta == null || meta.getMetaType() == null ) {
            return val;
        }
        if (val == null) {
            return "-";
        }
        return AmountType.parseByMeta(meta.getMetaType()).format(val);
    }

    public static Object formatterDateTime(Object value) {
        if (value instanceof Long) {
            if ((long)value < 1000000000) {
                return "";
            }
            if ((long)value > 10000000000L) {
                return DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_FORMAT,DateUtil.parseByMilliSeconds((long)value));
            } else {
                return DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_FORMAT,DateUtil.parseBySecond((long)value));
            }
        }
        if (value instanceof Integer) {
            return "";
        }
        return value;
    }

    public static Object formatterDate(Object value) {
        if (value instanceof Long) {
            if ((long)value < 1000000000) {
                return "";
            }
            if ((long)value > 10000000000L) {
                return DateUtil.formatLocalDateTime(DateUtil.DATE_DAY_FORMAT,DateUtil.parseByMilliSeconds((long)value));
            } else {
                return DateUtil.formatLocalDateTime(DateUtil.DATE_DAY_FORMAT,DateUtil.parseBySecond((long)value));
            }
        }
        if (value instanceof Integer) {
            return "";
        }
        return value;
    }
}
